using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Relation_Update_Delete
/// </summary>
public class RelationOperation
{
    public RelationOperation()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public void ApplyRelationOperation(ref ArrayList cmdArrayList, string KeyValue, DataTable PageInfo, DataTable InputInfo, String Operation)
    {
        DataRow[] drr = FetchRelationRecords(PageInfo.Rows[0]["page_id"].ToString(), Operation);
        if (drr.Length > 0)
        {
            if (InputInfo == null)
            {
                HDDatabase HDD = new HDDatabase();
                InputInfo = HDD.GetDataTable("select * from " + PageInfo.Rows[0]["page_table_name"].ToString().Replace("'", "''") + " where af_row_id='" + KeyValue + "'").Copy();
                HDD = null;
            }

            foreach (DataRow dr in drr)
            {
                foreach (DataRow drData in InputInfo.Rows)
                {
                    String sRelation_Table = "";
                    String sKeyValue = getAFROWID(dr, drData, ref sRelation_Table);
                    if (sKeyValue != "")
                    {
                        ApplyOperationOnKeyRecord(ref cmdArrayList, dr, drData, Operation, sRelation_Table, sKeyValue);
                    }
                }
            }
        }
    }

    private DataRow[] FetchRelationRecords(String Page_Id, String Operation)
    {
        DataRow[] drr = null;
        HDDatabase HDD = new HDDatabase();
        try
        {
            DataTable dtRelation = HDD.GetDataTable("select * from sy_relation where is_active='1' and operations like '%" + Operation + "'");
            drr = dtRelation.Select("source_table='" + Page_Id.Replace("'","''") + "'");
            dtRelation.Dispose();
            dtRelation = null;
        }

        catch
        {

        }

        finally
        {
            HDD = null;
        }
        return drr;
    }

    private String getAFROWID(DataRow drRelation, DataRow drData, ref String Relation_Table)
    {
        String sReturn = "";
        HDDatabase HDD = new HDDatabase();
        try
        {
            Relation_Table=HDD.GetColumnValue("select page_table_name from sy_pg where page_id='" + drRelation["relation_table"].ToString().Replace("'","''") + "'");
            //if (drRelation["relation_field"].ToString().ToLower().Trim() == drRelation["matching_relation_field"].ToString().ToLower().Trim())
            //{
            //    String sSourceTable = HDD.GetColumnValue("select page_table_name from sy_pg where page_id='" + drRelation["source_table"].ToString().Replace("'","''") + "'");
            //    String sOldValue = HDD.GetColumnValue("select " + drRelation["source_field"].ToString().Replace("'", "''") + " from " + sSourceTable.Replace("'", "''") + " where af_row_id='" + drData["af_row_id"].ToString() + "'");
            //    sSql = "select af_row_id from " + Relation_Table + " where " + drRelation["matching_relation_field"].ToString() + "='" + sOldValue + "'";
            //}
            //else
            //{
            //     sSql="select af_row_id from " + Relation_Table + " where " + drRelation["matching_relation_field"].ToString() + "='" + drData[drRelation["matching_source_field"].ToString()].ToString().Replace("'", "''") + "'";
            //}
            String sSourceTable = HDD.GetColumnValue("select page_table_name from sy_pg where page_id='" + drRelation["source_table"].ToString().Replace("'", "''") + "'");
            String sOldValue = HDD.GetColumnValue("select " + drRelation["matching_source_field"].ToString().Replace("'", "''") + " from " + sSourceTable.Replace("'", "''") + " where af_row_id='" + drData["af_row_id"].ToString() + "'");
            String sSql = "select af_row_id from " + Relation_Table + " where " + drRelation["matching_relation_field"].ToString() + "='" + sOldValue.Replace("'","''") + "'";
            if (drRelation["condition"].ToString().Trim() != "")
            {
                sSql = sSql + " and " + drRelation["condition"].ToString().Trim();
            }
            sReturn = HDD.GetColumnValue(sSql);
        }
        catch
        {

        }
        finally
        {
        }
        return sReturn;
    }

    private void ApplyOperationOnKeyRecord(ref ArrayList cmdArrayList,DataRow drRelation,DataRow drData, String Operation, String Relation_Table, String Key_Value)
    {
        String sSql = "";
        switch (Operation.ToUpper())
        {
            case "UPDATE":
                sSql = "update [" + Relation_Table + "] set " + drRelation["relation_field"].ToString().Replace("'", "''") + "='" + drData[drRelation["source_field"].ToString()].ToString().Replace("'", "''") + "' where af_row_id='" + Key_Value + "'";
                cmdArrayList.Add(new SqlCommand(sSql));
                break;
            case "DELETE":
                sSql = "delete from [" + Relation_Table + "] where af_row_id='" + Key_Value.Replace("'","''") + "'";
                cmdArrayList.Add(new SqlCommand(sSql));
                DeleteInput DI = new DeleteInput();
                HDDatabase HDD = new HDDatabase();
                DI.DeleteChildRecords(ref cmdArrayList, HDD.GetDataTable("select * from sy_pg where page_id='" + drRelation["relation_table"].ToString() + "'"), Key_Value, HDD);
                DI = null;
                HDD = null;
                break;
            default:
                break;
        }
    }
}
